package by.yakusekmaxim.bookgid.webapp.page.admin.book;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.datamodel.Author;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Genre;
import by.yakusekmaxim.bookgid.datamodel.Series;
import by.yakusekmaxim.bookgid.datamodel.enums.PublicationStatus;
import by.yakusekmaxim.bookgid.services.AuthorService;
import by.yakusekmaxim.bookgid.services.BookService;
import by.yakusekmaxim.bookgid.services.GenreService;
import by.yakusekmaxim.bookgid.services.SeriesService;
import by.yakusekmaxim.bookgid.webapp.page.AdminLayout;
import by.yakusekmaxim.bookgid.webapp.page.admin.image.InsertImagePanel;
import by.yakusekmaxim.bookgid.webapp.page.utils.renderer.AuthorRenderer;
import by.yakusekmaxim.bookgid.webapp.page.utils.renderer.GenreRenderer;
import by.yakusekmaxim.bookgid.webapp.page.utils.renderer.PublicationStatusRenderer;
import by.yakusekmaxim.bookgid.webapp.page.utils.renderer.SeriesRenderer;

public class EditBookPage extends AdminLayout {
	@Inject
	private BookService bookService;
	
	@Inject
	private AuthorService authorService;
	
	@Inject
	private GenreService genreService;
	
	@Inject
	private SeriesService seriesService;


	private Book book;

	protected void onInitialize() {
		super.onInitialize();

		if (book.getId() == null) {
			add(new Label("pageNameLabel", new ResourceModel(
					"p.admin.editbook.pageName.newBook").getObject()));
		} else {
			add(new Label("pageNameLabel", new ResourceModel(
					"p.admin.editbook.pageName").getObject()
					+ book.getName()));
		}

		if (book.getStatus() == null) {
			PublicationStatus status = PublicationStatus.publicationstatus_edited;
			book.setStatus(status);
		}

		Form<Book> form = new Form<Book>("form",
				new CompoundPropertyModel<Book>(book));
		final Label tfId = new Label("id", book.getId());
		form.add(tfId);
		
		final TextField<String> tfName = new TextField<String>("name");
		tfName.add(new PropertyValidator<String>());
		form.add(tfName);

		final TextField<String> tfNameOr = new TextField<String>("nameOr");
		tfNameOr.add(new PropertyValidator<String>());
		form.add(tfNameOr);
		
		final DateTextField tfDate = new DateTextField("date");
		tfDate.add(new PropertyValidator<String>());
		form.add(tfDate);
		
		final TextArea<String> tfDescription = new TextArea<String>("description");
		tfDescription.add(new PropertyValidator<String>());
		form.add(tfDescription);
		
		List<Author> choicesAuthor  = authorService.getAll();
		form.add(new DropDownChoice<Author>("author", choicesAuthor,AuthorRenderer.INSTANCE));
		
		List<Genre> choicesGenre  = genreService.getAll();
		form.add(new DropDownChoice<Genre>("genre", choicesGenre,GenreRenderer.INSTANCE));
		
		List<Series> choicesSeries  = seriesService.getAll();
		form.add(new DropDownChoice<Series>("series", choicesSeries,SeriesRenderer.INSTANCE));
		
		List<PublicationStatus> choicesStatus = Arrays.asList(PublicationStatus
				.values());
		form.add(new DropDownChoice<PublicationStatus>("status", choicesStatus,
				PublicationStatusRenderer.INSTANCE));
		
		Label imageLabel = new Label("imageLabel", "");
		if (book.getImage() != null) {
			imageLabel.add(AttributeModifier.replace("src", book.getImage().getUrl()));
		} else {
			imageLabel
					.add(AttributeModifier.replace("style", "display: none;"));
		}
		imageLabel.setOutputMarkupId(true);
		form.add(imageLabel);
		
		final InsertImagePanel insertImage = new InsertImagePanel("insertImage",imageLabel ,book);
		insertImage.add(AttributeModifier.replace("style",
				"display: none;"));
		insertImage.setOutputMarkupId(true);
		add(insertImage);
		
		AjaxLink<Void> ajaxSelectPicture = new AjaxLink<Void>("selectPicture") {
			@Override
			public void onClick(AjaxRequestTarget target) {
				insertImage.add(AttributeModifier.replace("style",
						"display: block;"));
				target.add(insertImage);
			}

		};

		form.add(ajaxSelectPicture);
		
		form.add(new SubmitLink("cancel") {
			@Override
			public void onSubmit() {
				super.onSubmit();

					BookPage page = new BookPage();
					setResponsePage(page);
	
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				bookService.saveOrUpdate(book);

				BookPage page = new BookPage();

				// page.error("Custom error");
				page.info("Книга ID: " + book.getId() + " сохранён!");
				// page.warn("Custom warn");

				setResponsePage(page);
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);
	}
	
	public EditBookPage(final Book book) {

		this.book = book;

	}

	@Override
	protected WebMarkupContainer getTarget() {
		return targetBook;
	}
	
	protected IModel<String> getPageTitle() {
		return new ResourceModel("p.admin.editbook.pageTitle");
	}

}
